<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\facade\Builder;

class News extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\News';

    //是否写入日志：0不写入，1写入，2自定义写入；默认1
    //如果给定 2 自定义写入，则需要在主Model定义writeLog(静态)方法
    //如果给定 1 自动写入日志，无需定义此变量
    protected int $writeLog = 1;

    //日志描述匹配，默认会匹配title(名称)和code(编号)，如果没有此字段请给定唯一描述标识，否则只会记录标识ID
    //如果有title或者code字段，无需定义此变量，否则只会记录标识ID
    protected array $writeLogKey = ['key' => 'title,code', 'val' => '名称,编号'];

    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '标题', 'modify' => '2,input'])
            ->addColumn(['field' => 'desc', 'title' => '简介', 'modify' => '2,textarea'])
            ->addColumn(['field' => 'type', 'title' => '类型', 'modify' => '2,radio,getType'])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'modify' => '2,img,1', 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'content', 'title' => '详情', 'modify' => '2,rich_text', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 200])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete'])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSearch([
                ['title', '文章标题搜索', 'input', '', '', '', 'like']
            ])
            ->addSwitchTool([['name' => 'status', 'pk' => $this->pk, 'text' => '开启|关闭']])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->fetch();
    }


    public static function getType(): array
    {
        return [1 => '新闻', 2 => '公告'];
    }

}
